package com.example.bishe.web.c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.bishe.entity.Record;
import com.example.bishe.entity.Room;
import com.example.bishe.entity.User;
import com.example.bishe.entity.UserRoom;
import com.example.bishe.mapper.RoomMapper;
import com.example.bishe.service.RoomService;
import com.example.bishe.util.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * c端空间接口
 *
 * @author: LDH
 * @date: 2023年03月20日 23:31
 */
@RestController
@Api(value = "客户端查询", tags = {"查询用户空间"})
@RequestMapping("/c/room")
public class CRoomController {

    @Autowired
    private RoomService roomService;

    @ApiOperation("分页查询用户空间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "空间名称/描述", paramType = "query")
    })
    @GetMapping("/page")
    public RestResult<IPage<Room>> page(
            @RequestParam(required = false,defaultValue = "1")  int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String title
    ){
        IPage<Room> recordIPage = roomService.getCRoomPage(pageNum, pageSize, userId, title);
        return RestResult.get_success(recordIPage);
    }

    @ApiOperation("分页查询用户空间内成员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "roomId", value = "空间id", paramType = "query")
    })
    @GetMapping("/member")
    public RestResult<IPage<User>> member(
            @RequestParam(required = false,defaultValue = "1")  int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long roomId
    ){
        return RestResult.get_success(roomService.getUserOfRoom(pageNum,pageSize,roomId));
    }

    @ApiOperation("用户创建空间")
    @PostMapping("/add")
    public RestResult<String> add(@RequestBody Room room){
        int result = roomService.add(room);
        if (result == -1)
            return RestResult.set_success("等待管理员审批");
        if (result == -2)
            return RestResult.set_success("一个用户只能加入一个情侣空间");
        return RestResult.set_success("创建成功");
    }

    @ApiOperation("用户加入空间")
    @PostMapping("/join")
    public RestResult<String> join(@RequestBody UserRoom userRoom){
        int result = roomService.join(userRoom);
        if (result == -1)
            return RestResult.error_msg(null,"你已在该空间中",-1);
        if (result == -2)
            return RestResult.error_msg(null,"请勿重复申请",-2);
        if (result == -3)
            return RestResult.set_success("一个用户只能加入一个情侣空间");
        return RestResult.set_success("等待管理者同意");
    }

    @ApiOperation("用户退出、解散空间")
    @PostMapping("/delete")
    public RestResult<Void> delete(@RequestBody UserRoom userRoom){
        roomService.delete(userRoom);
        return RestResult.set_success("退出成功！");
    }


}